#!/bin/bash

batch_size=(5 25 125 625 3125 15625)
learning_rate=(0.02 0.04 0.08 0.16 0.32 0.64)

gsql -d postgres -p 26000 -c "CREATE TABLE health_data(
  HeartDisease INTEGER,
  BMI FLOAT,
  Smoking FLOAT,
  AlcoholDrinking FLOAT,
  Stroke FLOAT,
  PhysicalHealth FLOAT,
  MentalHealth FLOAT,
  DiffWalking FLOAT,
  Sex FLOAT,
  AgeCategory FLOAT,
  Race FLOAT,
  Diabetic FLOAT,
  PhysicalActivity FLOAT,
  GenHealth FLOAT,
  SleepTime FLOAT,
  Asthma FLOAT,
  KidneyDisease FLOAT,
  SkinCancer FLOAT
);"

gsql -d postgres -p 26000 -c "COPY health_data FROM '/home/omm/heart_.csv' WITH (FORMAT csv, HEADER);"
gsql -d postgres -p 26000 -c "UPDATE health_data SET
  BMI = (BMI - (SELECT MIN(BMI) FROM health_data)) /
        ((SELECT MAX(BMI) FROM health_data) - (SELECT MIN(BMI) FROM health_data)),
  PhysicalHealth = (PhysicalHealth - (SELECT MIN(PhysicalHealth) FROM health_data)) /
        ((SELECT MAX(PhysicalHealth) FROM health_data) - (SELECT MIN(PhysicalHealth) FROM health_data)),
  MentalHealth = (MentalHealth - (SELECT MIN(MentalHealth) FROM health_data)) /
        ((SELECT MAX(MentalHealth) FROM health_data) - (SELECT MIN(MentalHealth) FROM health_data)),
  SleepTime = (SleepTime - (SELECT MIN(SleepTime) FROM health_data)) /
  ((SELECT MAX(SleepTime) FROM health_data) - (SELECT MIN(SleepTime) FROM health_data)),
  Diabetic = (Diabetic - (SELECT MIN(Diabetic) FROM health_data)) /
        ((SELECT MAX(Diabetic) FROM health_data) - (SELECT MIN(Diabetic) FROM health_data)),
  GenHealth = (GenHealth - (SELECT MIN(GenHealth) FROM health_data)) /
        ((SELECT MAX(GenHealth) FROM health_data) - (SELECT MIN(GenHealth) FROM health_data)),
  AgeCategory = (AgeCategory - (SELECT MIN(AgeCategory) FROM health_data)) /
        ((SELECT MAX(AgeCategory) FROM health_data) - (SELECT MIN(AgeCategory) FROM health_data));
"

gsql -d postgres -p 26000 -c "create table batch(batch float,out float[5]);"
gsql -d postgres -p 26000 -c "create table rate(rate float,out float[5]);"

for j in {0..5}
do
	gsql -d postgres -p 26000 -c "CREATE MODEL health_lr_a$j USING logistic_regression FEATURES BMI,Smoking,AlcoholDrinking,Stroke,PhysicalHealth,MentalHealth,DiffWalking,Sex,AgeCategory,Race,Diabetic,PhysicalActivity,GenHealth,SleepTime,Asthma,KidneyDisease,SkinCancer 
TARGET HeartDisease FROM health_data2 WITH
  batch_size = ${batch_size[$j]},learning_rate = 0.5;"
	gsql -d postgres -p 26000 -c "insert into batch select hyperparametersvalues[1],trainingscoresvalue from gs_model_warehouse where  modelname='health_lr_a$j';"
done
	max_b=`gsql -d postgres -p 26000 -c "select batch from batch where ($1*out[1]+$2*out[3]+$3*out[4])=(select max($1*out[1]+$2*out[3]+$3*out[4]) from batch);"`

max_b=${max_b##*-}
max_b=${max_b%(*}
prime_bm=`gsql -d postgres -p 26000 -c "select * from batch;"`

for j in {0..5}
do
	gsql -d postgres -p 26000 -c "CREATE MODEL health_lr_b$j USING logistic_regression FEATURES BMI,Smoking,AlcoholDrinking,Stroke,PhysicalHealth,MentalHealth,DiffWalking,Sex,AgeCategory,Race,Diabetic,PhysicalActivity,GenHealth,SleepTime,Asthma,KidneyDisease,SkinCancer
TARGET HeartDisease FROM health_data2 WITH
  batch_size = $max_b,
  learning_rate = ${learning_rate[$j]};"
	gsql -d postgres -p 26000 -c "insert into rate select hyperparametersvalues[3],trainingscoresvalue from gs_model_warehouse where  modelname='health_lr_b$j';"
done
	max_l=`gsql -d postgres -p 26000 -c "select rate from rate where ($1*out[1]+$2*out[3]+$3*out[4])=(select max($1*out[1]+$2*out[3]+$3*out[4]) from rate);"`
max_l=${max_l##*-}
max_l=${max_l%(*}

prime_l=`gsql -d postgres -p 26000 -c "select * from rate;"`


gsql -d postgres -p 26000 -c "drop table batch;"
gsql -d postgres -p 26000 -c "drop table rate;"

for j in {0..5}
do
gsql -d postgres -p 26000 -c "drop model health_lr_a$j;"
gsql -d postgres -p 26000 -c "drop model health_lr_b$j;"
done

echo $prime_bm > out.txt
echo $prime_l >> out.txt

echo $max_b
echo $max_l

gsql -d postgres -p 26000 -c "drop table health_data"


